package com.redijedi.tapestry5.internal;

import java.util.Collection;
import java.util.HashMap;
import java.util.Map;

/**
 * @author torr
 * 
 */
public class InternalUtils {

	/**
	 * Converts an array of type String into a Map of String types. Every
	 * sequential pair will become key-value pairs in the Map returned.
	 * 
	 * @param strings
	 * @return
	 */
	public static Map<String, String> mapFromKeysAndValues(String... strings) {
		Map<String, String> map = new HashMap<String, String>();

		for (int i = 0; i < strings.length;) {
			if (strings.length >= i + 2) {
				map.put(strings[i], strings[++i]);
			} else {
				map.put(strings[i++], "");
			}
		}

		return map;
	}

	/**
	 * Converts a string to a valid identifier string. This includes removing
	 * invalid characters.
	 * 
	 * @param s
	 *            The String to convert to an identifier.
	 * @return The converted String.
	 */
	public static String convertStringToIdentifier(String s) {
		return s.replace(" ", "");
	}

	/**
	 * This will try to convert a given Object to it's string representation.
	 * This should provide a reasonable default for non-collection objects.
	 * Collections are ignored and null is returned.
	 * 
	 * @param o
	 * @return
	 */
	private static String attemptToConvertToString(Object o) {
		if (o instanceof Collection) {
			return null;
		} else {
			return o.toString();
		}
	}
}
